//
//  ChildBaseViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/9/3.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//
/**
 *   子类写的xcodetableView
 *
 */
#import "ChildBaseViewController.h"
#import "UIParameter.h"
#import "nextDetailZhuanJiaViewController.h"
#import "zhuanJiaJiangTang.h"
#import "TableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "Macros.h"
#import "CustomButton.h"
#import "HttpRequest.h"
#import "TCLYUser.h"

@interface ChildBaseViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    NSString *indexTag;
}
@property (nonatomic, strong) UITableView *myTableView;
@property(nonatomic,copy)NSMutableArray *tableViewArray;
@property(nonatomic,retain) TableViewCell *myCell;
@property(nonatomic,retain)UILabel *zanNumLabel;
@property(nonatomic,retain)CustomButton *shouBtn;
//图片的位置
@property(nonatomic,retain)UIImageView *pictureImageView01;
@property(nonatomic,retain)UIImageView *pictureImageView02;
@property(nonatomic,retain)UIImageView *pictureImageView03;
//日期
@property(nonatomic,retain)UILabel *dateLabel;
//价格
@property(nonatomic,retain)UILabel *priceLabel;
//dateLabel_y
@property(nonatomic,assign)float dateLabel_y;
@end

@implementation ChildBaseViewController
@synthesize tableViewArray,myCell,zanNumLabel,shouBtn,pictureImageView01,pictureImageView02,pictureImageView03,dateLabel,priceLabel,dateLabel_y;
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)createTableViewFromVC:(NSString *)yourTag
{
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    indexTag = yourTag;
}
//将子类的数组负值给父类
-(void)changeArray:(NSMutableArray*)array
{
    tableViewArray=array;
//    NSLog(@"tableViewArray=%@",tableViewArray);
}
#pragma mark - myTableView
- (UITableView *)myTableView
{
    if (!_myTableView)
    {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FUll_VIEW_WIDTH, FUll_CONTENT_HEIGHT - PageBtn-0.232*screenWidth) style:UITableViewStylePlain];
    }
    
    return _myTableView;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return 20;
//    NSLog(@"tableViewArray.count=%ld",tableViewArray.count);
    return tableViewArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    zhuanJiaJiangTang *zjjh=[zhuanJiaJiangTang shardeManager];
    myCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (myCell == nil)
    {
        myCell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil]lastObject];
        myCell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (tableViewArray.count>0)
    {

//        note_comment->讨论人数
        zjjh.note_comment=[tableViewArray[indexPath.row] objectForKey:@"note_comment"];
        zjjh.note_describle=[tableViewArray[indexPath.row] objectForKey:@"note_describle"];
        zjjh.note_id=[tableViewArray[indexPath.row] objectForKey:@"note_id"];
        zjjh.note_look=[tableViewArray[indexPath.row] objectForKey:@"note_look"];
        zjjh.note_name=[tableViewArray[indexPath.row] objectForKey:@"note_name"];
        zjjh.user_id=[tableViewArray[indexPath.row] objectForKey:@"user_id"];
        zjjh.user_pic=[tableViewArray[indexPath.row] objectForKey:@"user_pic"];
        zjjh.username=[tableViewArray[indexPath.row] objectForKey:@"username"];
        zjjh.zhanColor=[tableViewArray[indexPath.row] objectForKey:@"note_zan"];
        zjjh.note_time=[tableViewArray[indexPath.row] objectForKey:@"note_time"];
        zjjh.note_price=[tableViewArray[indexPath.row] objectForKey:@"note_price"];
        zjjh.picArray=[tableViewArray[indexPath.row] objectForKey:@"pic"];
        
    if (![zjjh.note_time isKindOfClass:[NSNull class]])
        {
            dateLabel.text=zjjh.note_time;
        }
        if (![zjjh.note_price isKindOfClass:[NSNull class]])
        {
            priceLabel.text=[NSString stringWithFormat:@"¥  %@",zjjh.note_price];
        }
//        NSLog(@"zjjh.zhanColor=%@",zjjh.zhanColor);
        if (![zjjh.note_name isKindOfClass:[NSNull class]])
        {
            myCell.titleLabel.text=zjjh.note_name;
        }
        if (![zjjh.username isKindOfClass:[NSNull class]])
        {
            myCell.nameLabel.text=zjjh.username;
        }
        if (![zjjh.note_comment isKindOfClass:[NSNull class]])
        {
            myCell.numLabel.text=zjjh.note_comment;
//            myCell.numLabel.text=@"20";
        }
        if (![zjjh.note_describle isKindOfClass:[NSNull class]])
        {
            myCell.contentLabel.text=zjjh.note_describle;
        }
        //添加 价格 Label
        float priceLabel_y=myCell.nameLabel.frame.origin.y;
        priceLabel =[[UILabel alloc]initWithFrame:CGRectMake(screenWidth-150, priceLabel_y, 140, 21)];
        priceLabel.textColor=pinkColor;
        priceLabel.text=[NSString stringWithFormat:@"¥ 34234"];
        priceLabel.font=[UIFont systemFontOfSize:16.0];
        priceLabel.textAlignment=NSTextAlignmentRight;
        [myCell.contentView addSubview:priceLabel];
        
        //自适应高度
        myCell.contentLabel.font = [UIFont systemFontOfSize:13];
        myCell.contentLabel.numberOfLines = 100;
        CGFloat height=[UILabel getHeightByWidth:self.myCell.contentLabel.frame.size.width title:self.myCell.contentLabel.text font:self.myCell.contentLabel.font];
        myCell.contentLabel.frame = CGRectMake(5, self.myCell.contentLabel.frame.origin.y, screenWidth-10, height);
        //三张图片放置的位置
        float pictureImageView_y=myCell.contentLabel.frame.origin.y+myCell.contentLabel.frame.size.height+5;
        pictureImageView01=[[UIImageView alloc]initWithFrame:CGRectMake(12, pictureImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//        pictureImageView01.backgroundColor=[UIColor orangeColor];
        [myCell.contentView addSubview:pictureImageView01];
        
        pictureImageView02=[[UIImageView alloc]initWithFrame:CGRectMake(12+(0.29867*screenWidth+2)*1, pictureImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//        pictureImageView02.backgroundColor=[UIColor orangeColor];
        [myCell.contentView addSubview:pictureImageView02];
        
        pictureImageView03=[[UIImageView alloc]initWithFrame:CGRectMake(12+(0.29867*screenWidth+2)*2, pictureImageView_y, 0.29867*screenWidth, 0.2*screenWidth)];
//        pictureImageView03.backgroundColor=[UIColor orangeColor];
        [myCell.contentView addSubview:pictureImageView03];
        
        //添加图片里的照片
        if ([zjjh.picArray isKindOfClass:[NSArray class]])
        {
            dateLabel_y=pictureImageView01.frame.origin.y+pictureImageView01.frame.size.height+5;
            pictureImageView01.hidden=NO;
            pictureImageView02.hidden=NO;
            pictureImageView03.hidden=NO;
            if (zjjh.picArray.count==1)
            {
                NSString *urlString0=zjjh.picArray[0];
                NSURL* imagePath0 = [NSURL URLWithString: urlString0];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
            }
            else if(zjjh.picArray.count==2)
            {
                NSString *urlString0=zjjh.picArray[0];
                NSString *urlString1=zjjh.picArray[1];
                NSURL* imagePath0 = [NSURL URLWithString: urlString0];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
                NSURL* imagePath1 = [NSURL URLWithString: urlString1];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView02 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];
                
            }
            else if(zjjh.picArray.count==3)
            {
                NSString *urlString0=zjjh.picArray[0];
                NSString *urlString1=zjjh.picArray[1];
                NSString *urlString2=zjjh.picArray[2];
                NSURL* imagePath0 = [NSURL URLWithString: urlString0];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView01 sd_setImageWithURL:imagePath0 placeholderImage:[UIImage imageNamed:@"default"]];
                NSURL* imagePath1 = [NSURL URLWithString: urlString1];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView02 sd_setImageWithURL:imagePath1 placeholderImage:[UIImage imageNamed:@"default"]];
                NSURL* imagePath2 = [NSURL URLWithString: urlString2];
                //给一张默认图片，先使用默认图片，当图片加载完成后再替换
                [pictureImageView03 sd_setImageWithURL:imagePath2 placeholderImage:[UIImage imageNamed:@"default"]];
            }
        }
        else
        {
            pictureImageView01.hidden=YES;
            pictureImageView02.hidden=YES;
            pictureImageView03.hidden=YES;
            dateLabel_y=myCell.contentLabel.frame.origin.y+myCell.contentLabel.frame.size.height+5;

        }

//        dateLabel_y=pictureImageView01.frame.origin.y+pictureImageView01.frame.size.height+5;
        
        dateLabel=[[UILabel alloc]initWithFrame:CGRectMake(12, dateLabel_y, screenWidth-100, 21)];
        dateLabel.textColor=[UIColor grayColor];
        dateLabel.text=@"1231312312312312221321";
        dateLabel.font=[UIFont systemFontOfSize:14.0f];
        [myCell.contentView addSubview:dateLabel];

        float shouBtn_y=dateLabel.frame.origin.y+dateLabel.frame.size.height+5;
//        float shouBtn_y=myCell.contentLabel.frame.origin.y+myCell.contentLabel.frame.size.height+5;
        shouBtn=[[CustomButton alloc]init];
        shouBtn.frame=CGRectMake(10, shouBtn_y,200,30);
        [shouBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        myCell.numLabel.frame=CGRectMake(screenWidth-210, shouBtn_y, myCell.numLabel.frame.size.width, myCell.numLabel.frame.size.height);
//******************************************************
//添加小手图片和点赞的数量
        int num=[zjjh.zhanColor intValue];
        shouBtn.tag=[zjjh.note_id intValue];
//        NSLog(@"shouBtn.tag=%ld",shouBtn.tag);
        NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
        [userDefaults setObject:zjjh.zhanColor forKey:zjjh.note_id];
        //like 是灰色的手,likeClick是带颜色的手
        if (num==0)
        {
            [shouBtn setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];

        }
        else
        {
            [shouBtn setImage:[UIImage imageNamed:@"likeClick"] forState:UIControlStateNormal];
        }
        [shouBtn setTitle:zjjh.note_look forState:UIControlStateNormal];
        [self.myCell.contentView addSubview:shouBtn];
//        NSLog(@"screenWidth-120=%f",screenWidth-120);
//         myCell.numLabel01.text=@"20";
         myCell.numLabel01.frame=CGRectMake(screenWidth-60, shouBtn_y, myCell.numLabel01.frame.size.width, myCell.numLabel01.frame.size.height);
        CGRect rect = myCell.frame;
//        rect.size.height = myCell.contentLabel.frame.size.height+105+21+5+0.2*screenWidth;
        rect.size.height = shouBtn.frame.size.height+shouBtn.frame.origin.y;
        myCell.frame = rect;
    }
    NSURL* imagePath = [NSURL URLWithString:zjjh.user_pic];
    //给一张默认图片，先使用默认图片，当图片加载完成后再替换
    [myCell.picImageView sd_setImageWithURL:imagePath placeholderImage:[UIImage imageNamed:@"default"]];
    return myCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    myCell=[self tableView:tableView cellForRowAtIndexPath:indexPath];
    //    self.myCell=[self tableview];
    return myCell.frame.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    zhuanJiaJiangTang *zjjh=[zhuanJiaJiangTang shardeManager];
    zjjh.user_pic=[tableViewArray[indexPath.row]objectForKey:@"user_pic"];
    zjjh.username=[tableViewArray[indexPath.row]objectForKey:@"username"];
    zjjh.note_name=[tableViewArray[indexPath.row]objectForKey:@"note_name"];
//    NSLog(@"zjjh.note_name111=%@",zjjh.note_name);
    zjjh.note_describle=[tableViewArray[indexPath.row]objectForKey:@"note_describle"];
    zjjh.note_id=[tableViewArray[indexPath.row]objectForKey:@"note_id"];
    zjjh.picArray=[tableViewArray[indexPath.row]objectForKey:@"pic"];
    
    nextDetailZhuanJiaViewController *ndzjvc=[nextDetailZhuanJiaViewController new];
    [self.navigationController pushViewController:ndzjvc animated:YES];
}

- (void)testAction
{
    if (isDebug)
    {
        NSLog(@"测试点击事件");
    }
}
-(void)onClick:(UIButton*)sender
{
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    int number=(int)sender.tag;
    NSLog(@"number=%d",number);
    NSString *num_id=[NSString stringWithFormat:@"%d",number];
    int num00=[[userDefaults objectForKey:num_id]intValue];
    NSString*str=sender.titleLabel.text;
    int num=[str intValue];
//    NSLog(@"numnum=%d",num);
    if (num00==0)
    {
        [sender setImage:[UIImage imageNamed:@"likeClick"] forState:UIControlStateNormal];
        num++;
//        myCell.numLabel.text=[NSString stringWithFormat:@"@%d",num];
        str=[NSString stringWithFormat:@"%d",num];
        [sender setTitle:str forState:UIControlStateNormal];
        [userDefaults setObject:@"1" forKey:num_id];
        NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/note/zan"];
        NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
        zhuanJiaJiangTang *zjjt=[zhuanJiaJiangTang shardeManager];
        parameters[@"uid"]=userID;
        parameters[@"note_id"]=[NSString stringWithFormat:@"%d",number];
        NSLog(@"note_id=%@",zjjt.note_id);
//        NSLog(@"zjjt.note_id=%@",zjjt.note_id);
        HttpRequest *httpRequest=[HttpRequest sharedInstance];
        [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
         {
             id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
             NSLog(@"json=%@",json);
         } failure:^(NSError *error) {
             NSLog(@"error=%@",error);
         }];

    }else if (num00==1)
    {
        [self getDataFromNet];
        [sender setImage:[UIImage imageNamed:@"like"] forState:UIControlStateNormal];
        num--;
//        myCell.numLabel.text=[NSString stringWithFormat:@"@%d",num];
        str=[NSString stringWithFormat:@"%d",num];
        [sender setTitle:str forState:UIControlStateNormal];
        [userDefaults setObject:@"0" forKey:num_id];
        
        NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/note/zan"];
        NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
        zhuanJiaJiangTang *zjjt=[zhuanJiaJiangTang shardeManager];
        parameters[@"uid"]=userID;
        parameters[@"note_id"]=[NSString stringWithFormat:@"%d",number];
//        NSLog(@"zjjt.note_id=%@",zjjt.note_id);
        HttpRequest *httpRequest=[HttpRequest sharedInstance];
        [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
         {
             id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
//             NSLog(@"json=%@",json);
         } failure:^(NSError *error)
        {
             NSLog(@"error=%@",error);
         }];
    }
}
-(void)getDataFromNet
{
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/admin/note/zan"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    zhuanJiaJiangTang *zjjt=[zhuanJiaJiangTang shardeManager];
    parameters[@"uid"]=userID;
    parameters[@"note_id"]=zjjt.note_id;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
